import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:test_flutter/constant/resources.dart';
import 'package:test_flutter/model/LanguageModel.dart';
import 'package:test_flutter/pages/Base/LBaseState.dart';
import 'package:test_flutter/utils/common_utils/NoticeTest.dart';
import 'package:test_flutter/utils/common_utils/SpHelper.dart';
import 'package:test_flutter/utils/common_utils/SpUtil.dart';
import 'package:test_flutter/utils/common_utils/common.dart';
import 'package:test_flutter/utils/common_utils/log_util.dart';
import 'package:test_flutter/utils/common_utils/object_util.dart';
import 'package:test_flutter/utils/factory/Fit.dart';

class LanguagePage extends StatefulWidget {


  @override
  State<StatefulWidget> createState() {
    return _LanguagePageState();
  }
}

class _LanguagePageState extends LBaseState<LanguagePage> {
  List<LanguageModel> _list = new List();

  LanguageModel _currentLanguage;

  @override
  void initState() {
    super.initState();

    _list.add(LanguageModel(StringIds.languageAuto, '', ''));
    _list.add(LanguageModel(StringIds.languageZH, 'zh', 'CH'));
   /* _list.add(LanguageModel(StringIds.languageTW, 'zh', 'TW'));
    _list.add(LanguageModel(StringIds.languageHK, 'zh', 'HK'));*/

    _list.add(LanguageModel(StringIds.languageMS, 'ms', 'MY'));
    _list.add(LanguageModel(StringIds.languageEN, 'en', 'US'));
    _list.add(LanguageModel(StringIds.languageTH, 'th', 'TH'));


    _currentLanguage = SpHelper.getLanguageModel();
    if (ObjectUtil.isEmpty(_currentLanguage)) {
      _currentLanguage = _list[0];
    }

    _updateData();
  }

  void _updateData() {
    LogUtil.e('currentLanguage: ' + _currentLanguage.toString());
    String language = _currentLanguage.countryCode;
    for (int i = 0, length = _list.length; i < length; i++) {
      _list[i].isSelected = (_list[i].countryCode == language);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Theme(
      data: new ThemeData(
        primarySwatch: Colors.red,
        brightness: Brightness.light,
      ),
      child: new Scaffold(
        appBar: PreferredSize(child: Container(
          decoration: BoxDecoration(
              gradient:  LinearGradient(colors: [
                Color(0xFF00E579),
                Color(0xFF00E585),
                Color(0xFF00E792),
                Color(0xFF00E8A4),
                Color(0xFF00ECBA)
              ])),
          child:  new AppBar(
            primary: true,
            title: new Text(
              cl.getString(StringIds.titleLanguage),
              style: new TextStyle(fontSize: 16.0),
            ),
            backgroundColor: Colors.transparent,
            actions: [
              new Padding(
                padding: EdgeInsets.all(12.0),
                child: new SizedBox(
                  width: 64.0,
                  child: new RaisedButton(
                    textColor: Colors.white,
                    color: Colors.redAccent,
                    child: Text(
                      cl.getString(StringIds.save),
                      style: new TextStyle(fontSize: 12.0),
                    ),
                    onPressed: () {
                      SpUtil.putString(
                          Constant.keyLanguage,
                          ObjectUtil.isEmpty(_currentLanguage.languageCode)
                              ? ''
                              : json.encode(_currentLanguage));
                      NoticeTest.getInstance().ctrl.add(null);
                      Navigator.pop(context);
                    },
                  ),
                ),
              ),
            ],
          ),
        ), preferredSize: new Size(Fit.scaleX(1080), kToolbarHeight)),
        body: Container(
          color: Colors.white,
          child: new ListView.builder(
              itemCount: _list.length,
              itemBuilder: (BuildContext context, int index) {
                LanguageModel model = _list[index];
                return new ListTile(
                  title: new Text(
                    (model.titleId == StringIds.languageAuto
                        ? cl.getString(model.titleId)
                        : cl.getString(model.titleId,
                        languageCode: 'zh', countryCode: 'CH')),
                    style: new TextStyle(fontSize: 13.0,color: Colors.black54),
                  ),
                  trailing: new Radio(
                      value: true,
                      groupValue: model.isSelected == true,
                      activeColor: Colors.green,
                      onChanged: (value) {
                        setState(() {
                          _currentLanguage = model;
                          _updateData();
                        });
                      }),
                  onTap: () {
                    setState(() {
                      _currentLanguage = model;
                      _updateData();
                    });
                  },
                );
              }),
        ),
      ),
    );
  }
}
